package com.zoe.onelink.enhancer.init;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartedEvent;

/**
 * <p>标题: 应用程序初始化抽象类</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-12-02
 */
@Slf4j
public abstract class AbstractApplicationInitializer implements ApplicationInitializer {


    @Override
    public void init(ApplicationStartedEvent event) throws Exception {
        long start = System.currentTimeMillis();
        String eventName = this.eventName() == null ? this.getClass().getName() : this.eventName();
        String asyncType = this.isAsync() ? "异步方式" : "同步方式";
        if (preInit(event)) {
            this.doInit(event);
        }
        long end = System.currentTimeMillis();
        log.info("[应用初始化事件] [{}] [{}] 初始化完成,耗时:{}ms", asyncType, eventName, end - start);
    }

    /**
     * 执行初始化
     *
     * @param event 容器事件
     * @throws Exception .
     */
    protected abstract void doInit(ApplicationStartedEvent event) throws Exception;


}
